﻿using System;

namespace ProxyPattern;

//代理模式
//抽象主题角色
public abstract class Person
{
    public abstract void BuyProduct();
}

//真实主题角色
public class RealBuyPerson : Person
{
    public override void BuyProduct()
    {
        Console.WriteLine("帮我买一部IPhone 15和一台Macbook Air");
    }
}

//代理角色
public class Friend : Person
{
    //引用真实主题实例
    private RealBuyPerson? _realSubject;

    public override void BuyProduct()
    {
        Console.WriteLine("通过代理访问真实实例对象的方法");
        _realSubject ??= new RealBuyPerson();

        PreBuyProduct();
        _realSubject.BuyProduct();
        PostBuyProduct();
    }

    //买东西之前需要做的
    private static void PreBuyProduct()
    {
        Console.WriteLine("寻找当地的直营店");
    }

    //买完之后需要做的
    private static void PostBuyProduct()
    {
        Console.WriteLine("通知委托人完成购买，并会在回国时带回物品");
    }
}

public static class Program
{
    public static void Main()
    {
        Person proxy = new Friend();
        proxy.BuyProduct();
    }
}
